//
//  SpeedHeaderView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/12.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "SpeedHeaderView.h"
#import "CardCollectionVIewLayout.h"
#import "CardCollectionViewCell.h"
#import "PopupView.h"
#import "TAPageControl.h"


#define ITEMCELLID  @"ITEMCELLID"
@interface SpeedHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource,CradListRowDelegate>{
    CGFloat          startX;
    CGFloat          endX;
    NSInteger        cellNumber;
    CardInforModel * selectModel;
}
@property (nonatomic,strong)UICollectionView * cardCollectionVIew;
@property (nonatomic,assign)NSInteger        currentIndex;
@end

@implementation SpeedHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self.dataSourceArr addObject:[CardInforModel new]];
        self.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
//        self.backgroundColor = kRandomColor;
        CardCollectionVIewLayout *layout = [[CardCollectionVIewLayout alloc]init];
        _cardCollectionVIew  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(400)) collectionViewLayout:layout];
        [self addSubview:_cardCollectionVIew];
        _cardCollectionVIew.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _cardCollectionVIew.delegate=self;
        _cardCollectionVIew.dataSource=self;
        _cardCollectionVIew.showsHorizontalScrollIndicator=NO;
        [_cardCollectionVIew registerClass:[CardCollectionViewCell class] forCellWithReuseIdentifier:ITEMCELLID];
        //
        TAPageControl * pageCountControl = [[TAPageControl alloc] initWithFrame:CGRectMake(0, kY_With_View(_cardCollectionVIew, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(23))];
        pageCountControl.numberOfPages = 4;
//        pageCountControl.currentDotImage = [UIImage imageNamed:@""];
//        pageCountControl.dotImage = [UIImage imageNamed:@""];
        pageCountControl.dotColor = kRandomColor;
        pageCountControl.dotSize = CGSizeMake(10, 10);
        pageCountControl.userInteractionEnabled = YES;
        pageCountControl.currentPage = 2;
        [self addSubview:pageCountControl];
    }
    return self;
}
#pragma mark =================== UICollectionViewDelegate,UICollectionViewDataSource ===================
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CardCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:ITEMCELLID forIndexPath:indexPath];
    [cell dataCount:self.dataSourceArr.count theSelfNum:indexPath.row];
    cell.card_inforModel = self.dataSourceArr[indexPath.row];
    cell.delegate = self;
    return cell;
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    startX = scrollView.contentOffset.x;
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    endX = scrollView.contentOffset.x;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self cellToCenter];
    });
}
-(void)cellToCenter{
    //最小滚动距离
    float  dragMinDistance = _cardCollectionVIew.bounds.size.width/20.0f;
    if (startX - endX >= dragMinDistance) {
        self.currentIndex -= 1; //向右
    }else if (endX - startX >= dragMinDistance){
        self.currentIndex += 1 ;//向左
    }
    NSInteger maxIndex  = [_cardCollectionVIew numberOfItemsInSection:0] - 1;
    self.currentIndex = self.currentIndex <= 0 ? 0 :self.currentIndex;
    self.currentIndex = self.currentIndex >= maxIndex ? maxIndex : self.currentIndex;
    [_cardCollectionVIew scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark -- CradListRowDelegate
/**
 @param c_row 卡片里面num
 @param f_row 卡片num
 */
-(void)chickRowNum:(NSInteger)c_row farthRowNum:(NSInteger)f_row{
    DLog(@"------------------------%ld=============%ld",c_row,f_row);
    selectModel = self.dataSourceArr[f_row];
    selectModel.rowNum = c_row;
    if (self.delegate && [self.delegate respondsToSelector:@selector(cardNumRowModel:)]) {
        [self.delegate cardNumRowModel:selectModel];
    }
}

-(void)addOneCardSelect:(NSInteger)copy_row{
    PopupView * popView = [[PopupView alloc] initPopupview:@[@"复制当前求购",@"新增一条"]];
    popView.Block_SelectType = ^(NSInteger row) {
        if (self.dataSourceArr.count > 4) {
            [GLGWProgressHUD showNotice:@"超过5个啦" view:nil];
            return;
        }
        if (row == 0) {
            CardInforModel * copyModel = [CardInforModel new];
            copyModel.unit = @"吨";
            copyModel = self.dataSourceArr[copy_row];
            [self.dataSourceArr addObject:copyModel];
        }else{
            @synchronized(self) {
                [self.dataSourceArr addObject:[CardInforModel new]];
            }
        }
        [self reloadView];
    };
}

-(void)deleteOne:(NSInteger)d_row{
    @synchronized(self) {
        [self.dataSourceArr removeObjectAtIndex:d_row];
    }
    [self reloadView];
}

-(void)reloadView{
    [self.cardCollectionVIew reloadData];
    [self.cardCollectionVIew scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.dataSourceArr.count-1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    self.currentIndex = self.dataSourceArr.count-1;
}
#pragma mark --- 懒加载
-(NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataSourceArr;
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGFloat offsetY = scrollView.contentOffset.y;
//    DLog(@"-------    %f",offsetY);
//}
-(void)relodCollection{
    [self.cardCollectionVIew reloadData];
}
@end
